<?php

namespace app\models;

use Yii;
use yii\base\Model;
use yii\helpers\Url;

class Page extends Model
{
	/*
		int count 列表总数
		int per 每页条数
		int front 当前页前有几个按钮
		int back 当前页后有几个按钮
	*/ 
	static function getPageHtml($count,$route,$per,$front,$back){
		$pages = ceil($count/$per);  // 获得总共的页数
		$current = Yii::$app->request->get('page');
		$current = $current?$current:1;
		$catid = Yii::$app->request->get('catid');

		if($current <= ($front+1)){
			$i = 1;
			$parts = $pages > ($front+$back+1)?($front+$back+1):$pages;
		}elseif($current > ($front+1) && $current < ($pages-$back+1)){
			$i = $current - $front;
			$parts = $current + $back;
		}else{
			$i = ($pages - ($front+$back)) < 1?1:($pages - ($front+$back));
			$parts = $i > ($pages-($front+$back+1))?$pages:($i+($front+$back+1));
		}
		$pageHtml ='<ul class="am-pagination">';
		if($current == 1){
			$pageHtml .='<li class="am-disabled"><a>首页</a></li>';
			$pageHtml .='<li class="am-disabled"><a>&laquo;</a></li>';
		}else{
			$pageHtml .='<li><a href="'.Url::to([$route,'page'=>1,'catid'=>$catid]).'">首页</a></li>';
			$pageHtml .='<li><a href="'.Url::to([$route,'page'=>$current-1,'catid'=>$catid]).'">&laquo;</a></li>';
		}

		for($i;$i<=$parts;$i++){
			if($i == $current){
				$pageHtml .= '<li class="am-active"><a>'.$i.'</a></li>';
				continue;
			}
			$pageHtml .= '<li><a href="'.Url::to([$route,'page'=>$i,'catid'=>$catid]).'">'.$i.'</a></li>';
		}
		if($current == $pages){
			$pageHtml .='<li class="am-disabled"><a>&raquo;</a></li>';
			$pageHtml .='<li class="am-disabled"><a>尾页</a></li>';

		}else{
			$pageHtml .='<li><a href="'.Url::to([$route,'page'=>$current+1,'catid'=>$catid]).'">&raquo;</a></li>';
			$pageHtml .='<li><a href="'.Url::to([$route,'page'=>$pages,'catid'=>$catid]).'">尾页</a></li>';

		}
		$pageHtml .='</ul>';
		return $pageHtml;
	}
}